package com.realphim.app;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.RelativeLayout;

import com.realphim.app.adapter.AdapterLeftMenu;
import com.realphim.app.model.ModelChild;
import com.realphim.app.model.ModelGroup;
import com.realphim.app.util.CommonDefine;
import com.realphim.app.util.JSONParser;

public class FragmentLeftMenu extends Fragment implements CommonDefine {

    private final String TAG = "FragmentLeftMenu";

    private RelativeLayout rlHome;

    private ExpandableListView elvMenu;

    private List<ModelGroup> listGroup;

    private AdapterLeftMenu adapterLeftMenu;

    private ProgressDialog mDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listGroup = new ArrayList<ModelGroup>();
        listGroup.add(new ModelGroup(0, "PHIM LẺ"));
        listGroup.add(new ModelGroup(1, "PHIM BỘ"));
        listGroup.add(new ModelGroup(2, "SHOW"));
        listGroup.add(new ModelGroup(3, "CLIP"));
        adapterLeftMenu = new AdapterLeftMenu(getActivity(), listGroup);

        mDialog = new ProgressDialog(getActivity());
        mDialog.setCancelable(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_left_menu, null);
        rlHome = (RelativeLayout) view.findViewById(R.id.rlHome);
        elvMenu = (ExpandableListView) view.findViewById(R.id.elvMenu);
        elvMenu.setAdapter(adapterLeftMenu);

        elvMenu.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                if (adapterLeftMenu.getGroup(groupPosition).listChild.size() == 0) {
                    new AsyntaskLoadChild().executeOnExecutor(
                            AsyncTask.THREAD_POOL_EXECUTOR, groupPosition);
                }
            }
        });

        rlHome.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ((ActivityMain) getActivity()).menu.showContent();
            }
        });

        // elvMenu.setOnChildClickListener(new OnChildClickListener() {
        //
        // @Override
        // public boolean onChildClick(ExpandableListView parent, View v,
        // int groupPosition, int childPosition, long id) {
        // Log.d("Click on child", "" + childPosition);
        // ((ActivityMain) getActivity()).menu.showContent();
        // return false;
        // }
        // });

        // elvMenu.expandGroup(0);
        return view;
    }

    private class AsyntaskLoadChild extends
            AsyncTask<Integer, Integer, Boolean> {

        int position;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mDialog.show();
        }

        @Override
        protected Boolean doInBackground(Integer... params) {
            position = params[0];

            String strUrl = SERVICE_GET_LIST_CATEGORY + ServiceParam.TYPE + "="
                    + listGroup.get(position).type;
            String jsonResult = new JSONParser().getJSONFromUrl(strUrl,
                    METHOD_GET, null);
            Log.i(TAG, jsonResult);
            try {
                JSONArray arrResult = new JSONArray(jsonResult);
                for (int i = 0; i < arrResult.length(); i++) {
                    JSONObject obj = arrResult.getJSONObject(i);
                    ModelChild child = new ModelChild();
                    child.id = obj.getInt("id");
                    child.title = obj.getString("title");
                    adapterLeftMenu.getGroup(position).listChild.add(child);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            mDialog.dismiss();
            adapterLeftMenu.notifyDataSetChanged();
        }

    }

}
